package fab.msn
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;

	public class MSNFakeConnection extends EventDispatcher
	{
		private var localConnection:LocalConnection;
		private var id:String;
		private var connectionName:String = "fakemsn";
		private var callbackFunction:Function;
		public var isMaster:Boolean;
		
		public function MSNFakeConnection(id:String, callbackFunction:Function) 
		{
			this.id = id;
			this.callbackFunction = callbackFunction;
			
			localConnection = new LocalConnection();
			localConnection.send( connectionName + "_" + id + "_master", "init_connection", id );
			localConnection.addEventListener(StatusEvent.STATUS, onInitConnection );
		}
		private function connectAsMaster():void
		{
			connect( "master" );
			isMaster = true;
		}
		private function connectAsSlave():void
		{
			connect( "slave" );
			isMaster = false;
		}
		private function connect(type:String):void
		{
			//localConnection = new LocalConnection();
			localConnection.allowDomain("*");
			localConnection.client = this;// callback methods on Client
			localConnection.addEventListener(StatusEvent.STATUS, onDataReceived );
			try {
				localConnection.connect( connectionName + "_" + id + "_" + type );
			} catch (error:ArgumentError) {
				
			}
		}
		
		// to be called by peer
		public function jsFunc(actions : String):void
		{
			callbackFunction( actions );
		}
		
		private function sendToMaster(funcStr:String,...rest:*):void
		{
			send( "master", funcStr, rest );
		}
		private function sendToSlave(funcStr:String,...rest:*):void
		{
			send( "slave", funcStr, rest );
		}		
		private function send( type:String, funcStr:String, ...rest:* ):void
		{
			localConnection.send( connectionName + "_" + id + "_" + type, funcStr, rest );
		}			
		public function call(funcStr:String,...rest:*):void
		{
			//trace("call", funcStr, rest);
			
			var msn_commands:Array = [ "getIsMaster", "getName", "getEmail", "getRemoteName", "getRemoteEmail", "jsFuncClose" ];
			if ( msn_commands.indexOf( funcStr ) != -1 )
			{
				switch( funcStr )
				{
					case "getIsMaster" :
					
					break;
				}
			}
			else {
				try {
					if ( isMaster )
					{
						sendToSlave( funcStr, rest );
					}
					else {
						sendToMaster( funcStr, rest );
					}
				} catch (error:ArgumentError) {
				}					
			}
		}
		
		public function init_connection(id:String):void
		{
		}
		private function onInitConnection(event:StatusEvent):void
		{
			localConnection.removeEventListener(StatusEvent.STATUS, onInitConnection );
			switch (event.level)
			{
				case "status":
				connectAsSlave();
				break;
				case "error":
				connectAsMaster();
				break;
			}
			dispatchEvent( new Event( "connected" )) ; 
		}
		private function onDataReceived(event:StatusEvent):void
		{
			localConnection.removeEventListener(StatusEvent.STATUS, onInitConnection );
			switch (event.level)
			{
				case "status":
				break;
				case "error":
				break;
			}
		}		
	}
}